﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace ATPlatforms.RabbitSkin.Serializers
{
    public class BinaryMessageSerializer<T> : ISerializeMessages<T>
    {
        BinaryFormatter formatter = new BinaryFormatter();

        public byte[] SerializeMessage(T message)
        {
            using (MemoryStream mStream = new MemoryStream())
            {
                formatter.Serialize(mStream, message);
                return mStream.ToArray();
            }
        }

        public T DeSerializeMessage(byte[] message)
        {
            MemoryStream memoryStream = new MemoryStream(message);
            return (T)formatter.Deserialize(memoryStream);
        }
    }
}
